<template>
  <div
    id="code-editor"
    ref="codeEditorRef"
    style="min-height: 600px; height: 90vh"
  ></div>
  <!--  <a-button @click="fillVal">填充值</a-button>-->
</template>
<script setup lang="ts">
import * as monaco from "monaco-editor";
import { onMounted, ref, toRaw, withDefaults, defineProps } from "vue";

const codeEditorRef = ref();
const codeEditor = ref();

/**
 * 定义组件属性类型
 *
 */
interface Props {
  value: string;
  language?: string;
  handleChange: (v: string) => void;
}

const props = withDefaults(defineProps<Props>(), {
  value: () => "",
  language: () => "java",
  handleChange: (v: string) => {
    // console.log(v);
  },
});

// const fillVal = () => {
//   if (!codeEditor.value) {
//     return;
//   }
//   //修改值
//   toRaw(codeEditor.value).setValue("新的值");
// };

//先加载，再挂载
onMounted(() => {
  if (!codeEditorRef.value) {
    return;
  }
  codeEditor.value = monaco.editor.create(codeEditorRef.value, {
    value: props.value,
    language: props.language,
    theme: "vs-dark",
    readOnly: false,
    minimap: {
      enabled: false,
      autohide: true,
      size: "proportional",
    },
    colorDecorators: true,
    selectionClipboard: true,
    automaticLayout: true,
    fontSize: 14,
  });
  //编辑、监听内容变化
  codeEditor.value.onDidChangeModelContent(() => {
    props.handleChange(toRaw(codeEditor.value).getValue());
    //监听的同时   显示值
  });
});
</script>

<style scoped></style>
